//
//  TimelineCell.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/18.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import SnapKit
class TimelineCell: UITableViewCell {

    var bottomViewTopConstraint : Constraint?
    
    var timeline : Timeline? {
        didSet {
            topView.timeline = timeline
            
            self.bottomViewTopConstraint?.uninstall()
            if let retweetStatus = timeline?.retweeted_status
            {
                //转发微博
               self.bottomView.snp_updateConstraints(closure: { (make) -> Void in
                     self.bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                retweetView.retweet = retweetStatus
                
                //显示转发微博
                retweetView.hidden = false
            }
                //原创微博
            else{
                self.bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraint = make.top.equalTo(topView.snp_bottom).constraint
                })
                
                //隐藏转发微博
                retweetView.hidden = true
            }
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)
        
        topView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
//            make.height.equalTo(100) 高度配图底部位置决定
        }
        
        //给转发微博设置约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(TimelineCellMargin)
            make.left.right.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            self.bottomViewTopConstraint = make.top.equalTo(topView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(40)//直接40高度
        }
        //必须给contentview设置了约束,预估高度那才能生效
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.left.right.equalTo(self)
        }
    }
    
    // MARK : 懒加载上下视图
    lazy var topView : TimelineCellTopView = TimelineCellTopView()
    
    lazy var bottomView : TimelineCellBottomView = TimelineCellBottomView()
    
    //转发视图(在topView下面)
    lazy var retweetView : TimelineRetweetView = TimelineRetweetView()
}
